import { MapBrowserEvent } from "ol";
import Feature, { FeatureLike } from "ol/Feature";
import { Geometry, SimpleGeometry } from "ol/geom";
import { Layer } from "ol/layer";
import { transform4326to3857 } from "../utils/transform";
import Style, { StyleLike } from "ol/style/Style";

const selectList: Layer[] = [];
export function getSelectItem(e: MapBrowserEvent<MouseEvent>): any | undefined {
  const item = getSelectInfo(e);
  if (item) {
    const {layer, feature, geometry } = item;
    const type = geometry.getType();
    if (["Point", "LineString"].includes(type)) {
      // (feature as Feature).set("scalable", !feature.get("scalable"));
      // const style = (feature as Feature).getStyle();
      // const styleFunction = (feature as Feature).getStyleFunction();

      // console.log(feature.get("scalable"),feature);
      
      
      
      // console.log(style);

     /*  const map = e.map;
      var resolution = map.getView().getResolution()!;
      // let getStyleOptions: void | Style | Style[] = []; 
      if (style) {
        if (typeof style === "function") {
          // getStyleOptions = style(feature, resolution);
          (feature as Feature).setStyle(style);
        } else if (Array.isArray(style)) {
          // getStyleOptions = style;
          (feature as Feature).setStyle(style);
        } else {
           style.getImage()?.setScale(2);
          // getStyleOptions.setImage();
          (feature as Feature).setStyle(style);
        }
      }  */

      
      // console.log();
      // console.log(feature);
      
      // console.log(styleFunction);
      
      /* if (style){
        console.log(style(feature, resolution), resolution);
      }  */
      
      selectList.push(layer);
      
      return feature.get("data");
    } else if (["MultiPoint", "MultiLineString"].includes(type)) {
      // console.log("点击到多个");
    }
  }

  if (selectList.length) {
    /* const layer = selectList.pop();
    console.log(layer!.getFeatures(transform4326to3857([113,40]))); */
    
    // layer.getA.set("scalable", true);
  }

  


}

export function getSelectInfo(e: MapBrowserEvent<MouseEvent>) {
  const map = e.map;
  return map.forEachFeatureAtPixel(
    e.pixel,
    function (feature: FeatureLike, layer: Layer, geometry: SimpleGeometry) {
      if (geometry) {
        return {
          layer,
          feature,
          geometry,
        };
      }
    }
  );
}
